Page({
  data: {
    rate: 5,
    content: '',
    images: [],
    isSubmitting: false,
    orderId: '',
    serviceId: ''
  },

  onLoad(options) {
    this.setData({
      orderId: options.orderId,
      serviceId: options.serviceId
    });
  },

  onRateChange(e) {
    this.setData({
      rate: e.detail.value
    });
  },

  onContentChange(e) {
    this.setData({
      content: e.detail.value
    });
  },

  onUploadSuccess(e) {
    const { files } = e.detail;
    this.setData({
      images: files
    });
  },

  onUploadFail(e) {
    wx.showToast({
      title: '上传失败',
      icon: 'none'
    });
  },

  onSubmit() {
    if (!this.data.content.trim()) {
      wx.showToast({
        title: '请填写评价内容',
        icon: 'none'
      });
      return;
    }

    this.setData({ isSubmitting: true });

    // 调用云函数提交评价
    wx.cloud.callFunction({
      name: 'addReview',
      data: {
        orderId: this.data.orderId,
        serviceId: this.data.serviceId,
        rate: this.data.rate,
        content: this.data.content,
        images: this.data.images.map(img => img.url)
      },
      success: () => {
        wx.showToast({
          title: '评价成功',
          icon: 'success'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      },
      fail: () => {
        wx.showToast({
          title: '评价失败',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ isSubmitting: false });
      }
    });
  }
});